﻿using C1.Phone.FlexGrid;
using C1.Silverlight.Resources;
using SmartMobile.Data;
using SMARTMobile.Models.Local;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.Models;
using SMARTMobile.Models.UI;

namespace SMARTMobile.ViewModels
{
    public class OutletMDContributionViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<OutletMDContributionExtend> _outletMDContributionObservable;
  
        #endregion

        public OutletMDContributionViewModel()
        {
            this.CreateConnection();
            this.GetOutletMDContribution();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<OutletMDContributionExtend> OutletMDContributionObservable
        {
            get { return this._outletMDContributionObservable; }
            set
            {
                this._outletMDContributionObservable = value;
                base.RaisePropertyChanged("OutletMDContributionObservable");
            }
        }

        #endregion

        #region Command
        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion

        #region Command Action Handlers
        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this._smartMobileContext.OutletMDContribution.DeleteAllOnSubmit(this._smartMobileContext.OutletMDContribution.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());       

                this._smartMobileContext.SubmitChanges();
            }

            List<OutletMDContribution> outletMDList = new List<OutletMDContribution>();

            double? sum = 0;
            foreach (var outletMD in this.OutletMDContributionObservable)
            {
                OutletMDContribution outletMDItem = new OutletMDContribution
                {
                    ID_PK = outletMD.ID_PK,
                    CustomerKey = outletMD.CustomerKey,
                    MDKey = outletMD.MDKey,
                    ContributionAmt = outletMD.ContributionAmt
                };
                outletMDList.Add(outletMDItem);
                sum += outletMD.ContributionAmt;
            }

            if (sum != 100)
            {
                this.ShowMessage("Tỉ lệ chưa đúng 100%");
                this.SaveCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this._smartMobileContext.OutletMDContribution.InsertAllOnSubmit(outletMDList);
            this._smartMobileContext.SubmitChanges();

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method

        private void GetOutletMDContribution()
        {
            // Tao danh sach mach dinh
            List<OutletMDType> mdTypeList = this._smartMobileContext.OutletMDType.ToList();
            if (mdTypeList.Count > 0)
            {
                this.OutletMDContributionObservable = new ObservableCollection<OutletMDContributionExtend>();
                foreach (var item in mdTypeList)
                {
                    OutletMDContributionExtend mdContribution = new OutletMDContributionExtend
                    {
                        ID_PK = Guid.NewGuid(),
                        MDKey = item.OutletTypeCode,
                        CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        ContributionAmt = 0,
                        OutletMDName = item.Description
                    };

                    this.OutletMDContributionObservable.Add(mdContribution);
                }
            }

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<OutletMDContribution> outletMDContributions = this._smartMobileContext.OutletMDContribution.Where(o => o.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                if (outletMDContributions.Count > 0)
                {
                    // Khi co du du lieu
                    if (outletMDContributions.Count == this.OutletMDContributionObservable.Count)
                    {
                        this.OutletMDContributionObservable = new ObservableCollection<OutletMDContributionExtend>();
                        foreach (var item in outletMDContributions)
                        {
                            OutletMDContributionExtend mdContribution = new OutletMDContributionExtend
                            {
                                ID_PK = item.ID_PK,
                                MDKey = item.MDKey,
                                CustomerKey = item.CustomerKey,
                                ContributionAmt = item.ContributionAmt,
                                OutletMDName = this.GetOutletMDName(item.MDKey)
                            };

                            this.OutletMDContributionObservable.Add(mdContribution);
                        }
                    }
                    else
                    {                        
                        foreach (var item in outletMDContributions)
                        {
                            OutletMDContributionExtend selectedItem = this.OutletMDContributionObservable.FirstOrDefault(o => (o.CustomerKey == item.CustomerKey && o.MDKey == item.MDKey));
                            if (selectedItem != null)
                            {
                                selectedItem.ID_PK = item.ID_PK;
                                selectedItem.MDKey = item.MDKey;
                                selectedItem.CustomerKey = item.CustomerKey;
                                selectedItem.ContributionAmt = item.ContributionAmt;
                                selectedItem.OutletMDName = this.GetOutletMDName(item.MDKey);
                            }
                        }

                        this._smartMobileContext.OutletMDContribution.DeleteAllOnSubmit(this._smartMobileContext.OutletMDContribution);
                        this._smartMobileContext.SubmitChanges();

                        List<OutletMDContribution> outletMDList = new List<OutletMDContribution>();
                        foreach (var outletMD in this.OutletMDContributionObservable)
                        {
                            OutletMDContribution outletMDItem = new OutletMDContribution
                            {
                                ID_PK = outletMD.ID_PK,
                                CustomerKey = outletMD.CustomerKey,
                                MDKey = outletMD.MDKey,
                                ContributionAmt = outletMD.ContributionAmt
                            };
                            outletMDList.Add(outletMDItem);                          
                        }
                        
                        this._smartMobileContext.OutletMDContribution.InsertAllOnSubmit(outletMDList);
                        this._smartMobileContext.SubmitChanges();
                    }                    
                }
                else
                {
                    List<OutletMDContribution> outletMDList = new List<OutletMDContribution>();

                    foreach (var outletMD in this.OutletMDContributionObservable)
                    {
                        OutletMDContribution outletMDItem = new OutletMDContribution
                        {
                            ID_PK = outletMD.ID_PK,
                            CustomerKey = outletMD.CustomerKey,
                            MDKey = outletMD.MDKey,
                            ContributionAmt = outletMD.ContributionAmt
                        };
                        outletMDList.Add(outletMDItem);
                    }

                    this._smartMobileContext.OutletMDContribution.InsertAllOnSubmit(outletMDList);
                    this._smartMobileContext.SubmitChanges();
                }
            }            
        }

        private string GetOutletMDName(string outletMDCode)
        {
            return this._smartMobileContext.OutletMDType.Where(o => o.OutletTypeCode == outletMDCode).Select(o => o.Description).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }
        #endregion
    }
}
